/*
 * ResourceGuildBanalce.cpp
 *
 *  Created on: 2014. 7. 28.
 *      Author: mega
 */

#include <algorithm>
#include <Database/Static.h>

#include "ResourceGuildBanalce.h"

namespace GuildServer { namespace NResource {

ResourceGuildBanalce::ResourceGuildBanalce()
{
}

ResourceGuildBanalce::~ResourceGuildBanalce()
{
	std::for_each(clsFlyMap.begin(), clsFlyMap.end(), [&](BALANCEMAP::value_type & pair) {
		delete pair.second;
	});
}

void
ResourceGuildBanalce::Clear()
{
	std::for_each(clsFlyMap.begin(), clsFlyMap.end(), [&](BALANCEMAP::value_type & pair) {
		delete pair.second;
	});
	clsFlyMap.clear();
}

bool
ResourceGuildBanalce::LoadResource()
{
	Database::ResultSet res = Database::Execute(static_cast<int>(DatabaseType::GUILD_MASTER), "SELECT * FROM resource_guild_balance");

	if (0 != res.GetSQLError())
		return false;

	if (0 == res.GetRowCount())
		return false;

	this->Clear();

	for (Database::ResultSet::iterator iter = res.begin(); iter != res.end(); iter++)
	{
		Flyweight * pFly = new Flyweight;
		std::string nationalCode;
		std::string desc;

		nationalCode	= iter->getString("NATIONAL_CODE");

		pFly->_INDEX	= iter->getInt("idx");
		pFly->_VALUE	= iter->getInt("value");

		desc			= iter->getString("desc");

		clsFlyMap[pFly->_INDEX] = pFly;
	}

	return true;
}

ResourceGuildBanalce::Flyweight *
ResourceGuildBanalce::GetFly(Balance eIdx)
{
	int nIdx = static_cast<int>(eIdx);

	auto iter = clsFlyMap.find(nIdx);
	if (iter == clsFlyMap.end())
		return NULL;

	return iter->second;
}

int
ResourceGuildBanalce::GetGuildMaxUser(int guild_grade)
{
	Flyweight * pFly = NULL;

	switch (guild_grade)
	{
	case 1 : pFly = GetFly(GUILD_1_GRADE_USER); break;
	case 2 : pFly = GetFly(GUILD_2_GRADE_USER); break;
	case 3 : pFly = GetFly(GUILD_3_GRADE_USER); break;
	case 4 : pFly = GetFly(GUILD_4_GRADE_USER); break;
	case 5 : pFly = GetFly(GUILD_5_GRADE_USER); break;
	case 6 : pFly = GetFly(GUILD_6_GRADE_USER); break;
	case 7 : pFly = GetFly(GUILD_7_GRADE_USER); break;
	case 8 : pFly = GetFly(GUILD_8_GRADE_USER); break;
	case 9 : pFly = GetFly(GUILD_9_GRADE_USER); break;
	case 10 : pFly = GetFly(GUILD_10_GRADE_USER); break;
	}

	if (NULL == pFly)
		return -1;

	return pFly->_VALUE;
}

int
ResourceGuildBanalce::GetGuildMaxScore()
{
	Flyweight * pFly = GetFly(GUILD_MAX_POINT);
	if (NULL == pFly)
		return -1;

	return pFly->_VALUE;
}

int
ResourceGuildBanalce::GetMemberDailyMaxScore()
{
	Flyweight * pFly = GetFly(GUILD_DAILY_MAX_POINT);
	if (NULL == pFly)
		return -1;

	return pFly->_VALUE;
}

int
ResourceGuildBanalce::GetGuildGradeByScore(int guild_score)
{
	if ( 0 >= guild_score )
		return 1; // added by spencer(2014.08.13), 혹시 음수일 때를 대비

	Flyweight * pFly = NULL;

	int grade = 1;

	for (; grade <= 10; grade++)
	{
		switch (grade)
		{
		case 1 : pFly = GetFly(GUILD_1_GRADE_POINT); break;
		case 2 : pFly = GetFly(GUILD_2_GRADE_POINT); break;
		case 3 : pFly = GetFly(GUILD_3_GRADE_POINT); break;
		case 4 : pFly = GetFly(GUILD_4_GRADE_POINT); break;
		case 5 : pFly = GetFly(GUILD_5_GRADE_POINT); break;
		case 6 : pFly = GetFly(GUILD_6_GRADE_POINT); break;
		case 7 : pFly = GetFly(GUILD_7_GRADE_POINT); break;
		case 8 : pFly = GetFly(GUILD_8_GRADE_POINT); break;
		case 9 : pFly = GetFly(GUILD_9_GRADE_POINT); break;
		case 10 : pFly = GetFly(GUILD_10_GRADE_POINT); break;
		}

		if (NULL == pFly)
			return -1;

		if (guild_score >= pFly->_VALUE)
			continue;

		break;
	}

	grade -= 1;

	return grade;
}

int
ResourceGuildBanalce::GetNextGradeGuildScore(int guild_grade)
{
	Flyweight * pFly = NULL;

	switch (guild_grade)
	{
	case 1 : pFly = GetFly(GUILD_2_GRADE_POINT); break;
	case 2 : pFly = GetFly(GUILD_3_GRADE_POINT); break;
	case 3 : pFly = GetFly(GUILD_4_GRADE_POINT); break;
	case 4 : pFly = GetFly(GUILD_5_GRADE_POINT); break;
	case 5 : pFly = GetFly(GUILD_6_GRADE_POINT); break;
	case 6 : pFly = GetFly(GUILD_7_GRADE_POINT); break;
	case 7 : pFly = GetFly(GUILD_8_GRADE_POINT); break;
	case 8 : pFly = GetFly(GUILD_9_GRADE_POINT); break;
	case 9 : pFly = GetFly(GUILD_10_GRADE_POINT); break;
	case 10 : pFly = GetFly(GUILD_MAX_POINT); break;
	}

	if (NULL == pFly)
		return -1;

	return pFly->_VALUE;
}

} /* namespace NResource */ } /* namespace GuildServer */
